//
//  ResponseSerialization.swift
//  flowerpot
//
//  Created by fyname on 16/1/25.
//  Copyright © 2016年 senhe. All rights reserved.
//

import Alamofire


//
//单个object对象序列化与反序列化
//
public protocol ResponseObjectSerializable{
    init?(response: NSHTTPURLResponse,respresentation: AnyObject)
}

extension Request {
    
    public func responseObject<T: ResponseObjectSerializable>(completionHandler: Response<T,NSError> ->Void) ->Self{
        
        let responseSerializer = ResponseSerializer<T,NSError> {
            request,response,data,error in
            guard error == nil
            else{
                return .Failure(error!)
            }
            
            let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
            let result = JSONResponseSerializer.serializeResponse(request,response,data,error)
            
            switch result{
            case .Success(let value):
                if let
                     response = response,
                    responseObject = T(response: response, respresentation: value){
                        return .Success(responseObject)
                        
                }else{
                    let failureReason = "JSON could not be serialized into response object: \(value)"
                    let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
                    return .Failure(error)
                }
                
            case .Failure(let error):
                return .Failure(error)
            }
        }
        
        return response(responseSerializer: responseSerializer,completionHandler: completionHandler)
        
    }
}




//
//集合对象序列化与反序列化
//
public protocol ResponseCollectionSerializable {
    static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [Self]
}

extension Alamofire.Request {
    public func responseCollection<T: ResponseCollectionSerializable>(completionHandler: Response<[T], NSError> -> Void) -> Self {
        let responseSerializer = ResponseSerializer<[T], NSError> { request, response, data, error in
            
            print("***********")
            print(response)
            print("***********")
            guard error == nil else { return .Failure(error!) }
            
            let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
            let result = JSONSerializer.serializeResponse(request, response, data, error)
            
            switch result {
            case .Success(let value):
                if let response = response {
                    print("*****value******")
                    print(value)
                    print("****value*******")
                    return .Success(T.collection(response: response, representation: value))
                } else {
                    let failureReason = "Response collection could not be serialized due to nil response"
                    let error = Error.errorWithCode(.JSONSerializationFailed, failureReason: failureReason)
                    return .Failure(error)
                }
            case .Failure(let error):
                return .Failure(error)
            }
        }
        
        return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
    }
}